package com.ejuornal.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.ejuornal.bean.Ads;
import com.ejuornal.bean.Artical;
import com.ejuornal.bean.Comment;
import com.ejuornal.bean.Periodical;
import com.ejuornal.bean.User;
import com.ejuornal.bean.vo.CommentUser;
import com.ejuornal.service.AdsService;
import com.ejuornal.service.ArticalService;
import com.ejuornal.service.CommentService;
import com.ejuornal.service.Comment_UserService;
import com.ejuornal.service.PeriodicalService;
import com.ejuornal.service.UserService;
import com.ejuornal.springutil.SpringUtil;
import com.google.gson.Gson;

public class ArticalReadAction extends BaseAction<CommentUser> {
	private static final long serialVersionUID = 1L;
	private ArticalService articalService;
	private PeriodicalService periodicalService;
	private CommentService commentService;
	private Comment_UserService comment_UserService;
	private UserService userService;
	private AdsService adsService;
	private Artical artical;
	private Periodical periodical;
	private Comment comment;
	private Ads ads;
	
	private String userName;
	private int articalId;
	private String content;//提交的文章评论内容 
	private String commentScore;//提交的评论文章得分
	private String articalUserName;//根据文章得到的文章作者名
	
	private List<Artical> recommendArtical;
	private List<Periodical> hotPeriodicals;
	private List<Comment> commentListInfo;
	private List<String> refDocList;//参考文献

	public String addAtical(){
		return SUCCESS;
	}
	
	//测试变量
	private String name ;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	/**
	 * descrption:文章详情页面内容读取
	 * author：lwf
	 * @return	 */
	public String getArticalDetial() {
		try {
			int articalReadCount = 0;
			int userId =  super.getUserIdBySession();
			userName = String.valueOf(userId);
			//文章评论
			comment_UserService = (Comment_UserService) SpringUtil.getBean("comment_UserService");
			String url ="artical/articalDetialInfo?articalId="+articalId+"&";
			String type="1";//定义type表示查询的是文章评论还是期刊评论---死的
			pages = comment_UserService.findCommentVOByTypeId((page-1)*5, url, type, articalId);
			//文章内容读取 -根据文章id
			artical = articalService.findById(articalId);//文章内容详情
			articalReadCount = artical.getViewCount();
			articalReadCount++;
			articalService.updateViewCount(articalId,articalReadCount);
			String refDoc = artical.getRefDoc();
			refDocList = splidRefDoc(refDoc);//参考文献
			int perPeriodicalId = artical.getPerPeriodicalId();
			periodical = periodicalService.findPeriodicalByPerId(perPeriodicalId);
			//根据文章中作者id得到作者姓名
			int articalUserId =artical.getUserId();
			User userInfo = userService.findById(articalUserId); 
			articalUserName = userInfo.getUserName();
			recommendArtical = articalService.findTop(10);//文章推荐，读取前10条	
			//期刊列表读取
			hotPeriodicals = periodicalService.findTop(10);//热门期刊，读取前10条
			
			ads = adsService.findAdsByPosIdAndTime(4);
			if(ads == null){
				ads = new Ads();
				ads.setPicSrc("attached/images/adsPic.jpg");
				ads.setUrl("javascript:void(0)");
			}
		} catch (Exception e) {
		}
		return "articalDetialInfo";
	}
	
	/**
	 *根据空格分隔指定字符串
	 *@author lwf
	 * @param doc 指定字符串
	 * @return List<String>
	 */
	public List<String> splidRefDoc(String doc){
		List<String> docRefList = new ArrayList<String>(); 
		String docSplid[] = doc.split("\\s+|,");
		for (String string : docSplid) {
			docRefList.add(string);
		}
		return docRefList;
	}
    /**
	 * description:文章评论
	 * author：lwf
	 * @return
	 */
	public String articalComment(){
		commentService = (CommentService) SpringUtil.getBean("commentService");
		//获取当前时间
		Calendar calendar = Calendar.getInstance();
		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH);
		int day = calendar.get(Calendar.DATE);
		String dateString = year+"-"+ (month + 1) +"-"+day;
		Date date = new Date();//util
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		try {
			date = simpleDateFormat.parse(dateString);
		} catch (ParseException e1) {
			e1.printStackTrace();
		}
		java.sql.Date dateSql = new java.sql.Date(date.getTime());
		
		int score = 0;
		if(commentScore == null || "".equals(commentScore))
		{
			score = 0;
		}else{
			score = Integer.parseInt(commentScore);//评论得分
		}
		int userId = super.getUserIdBySession();
		Comment commentSub =new Comment();
		commentSub.setContent(content);
		commentSub.setCommDate(dateSql);
		commentSub.setOpinion("赞成");
		commentSub.setScore(score);
		commentSub.setType("1");//type为评论类型 1 为文章
		commentSub.setTypeId(articalId);
		commentSub.setUserId(userId);//userId 需要获取到session
				
		commentService.add(commentSub);
		Gson son = new Gson();
		String aaaString = son.toJson(commentSub);
		HttpServletResponse res = ServletActionContext.getResponse();   
		res.reset();   
		res.setContentType("text/html;charset=utf-8");   
		PrintWriter pw;
		try {
			pw = res.getWriter();
			pw.print(aaaString);
			pw.flush();   
			pw.close();  
		} catch (IOException e) {
			e.printStackTrace();
		}   
		return "articalComment";
	}
	
	public Comment getComment() {
		return comment;
	}

	public void setComment(Comment comment) {
		this.comment = comment;
	}

	public List<Comment> getCommentListInfo() {
		return commentListInfo;
	}

	public void setCommentListInfo(List<Comment> commentListInfo) {
		this.commentListInfo = commentListInfo;
	}

	public List<Periodical> getHotPeriodicals() {
		return hotPeriodicals;
	}



	public void setHotPeriodicals(List<Periodical> hotPeriodicals) {
		this.hotPeriodicals = hotPeriodicals;
	}


	public Periodical getPeriodical() {
		return periodical;
	}



	public void setPeriodical(Periodical periodical) {
		this.periodical = periodical;
	}

	public List<Artical> getRecommendArtical() {
		return recommendArtical;
	}

	public void setRecommendArtical(List<Artical> recommendArtical) {
		this.recommendArtical = recommendArtical;
	}

	public int getArticalId() {
		return articalId;
	}

	public void setArticalId(int articalId) {
		this.articalId = articalId;
	}

	public ArticalService getArticalService() {
		return articalService;
	}
	@Resource(name="articalService")
	public void setArticalService(ArticalService articalService) {
		this.articalService = articalService;
	}

	public Artical getArtical() {
		return artical;
	}

	public void setArtical(Artical artical) {
		this.artical = artical;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public Comment_UserService getComment_UserService() {
		return comment_UserService;
	}
	public void setComment_UserService(Comment_UserService comment_UserService) {
		this.comment_UserService = comment_UserService;
	}
	public String getCommentScore() {
		return commentScore;
	}
	public void setCommentScore(String commentScore) {
		this.commentScore = commentScore;
	}
	public String getArticalUserName() {
		return articalUserName;
	}
	public void setArticalUserName(String articalUserName) {
		this.articalUserName = articalUserName;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public PeriodicalService getPeriodicalService() {
		return periodicalService;
	}
	@Resource(name="periodicalService")
	public void setPeriodicalService(PeriodicalService periodicalService) {
		this.periodicalService = periodicalService;
	}
	
	public UserService getUserService() {
        return userService;
    }
	@Resource(name="userService")
    public void setUserService(UserService userService) {
        this.userService = userService;
    }
	public AdsService getAdsService() {
		return adsService;
	}
	public void setAdsService(AdsService adsService) {
		this.adsService = adsService;
	}
	public Ads getAds() {
		return ads;
	}
	public void setAds(Ads ads) {
		this.ads = ads;
	}
	public List<String> getRefDocList() {
		return refDocList;
	}
	public void setRefDocList(List<String> refDocList) {
		this.refDocList = refDocList;
	}
}
